package com.cxhc.gank;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.cxhc.gank.imageWatcher.ImageWatcher;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements IFuLiCallback, FuliAdapter.OnPicClickCallback {

    RecyclerView rv;

    FuliAdapter adpter;

    ImageWatcher imageWatcher;
    public static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        rv = findViewById(R.id.rv);
        imageWatcher = findViewById(R.id.image_watcher);
        initWatcher();
        rv.setLayoutManager(new GridLayoutManager(this, 2));
//        rv.setLayoutManager(new LinearLayoutManager(this));
//        adpter = new FuliAdapter();
//        rv.setAdapter(adpter);
        rv.setAdapter(adpter = new FuliAdapter());
        adpter.setClickCallback(this);
        new NetUtils().getFuliPics(this);
    }

    private void initWatcher() {
        imageWatcher.setErrorImageRes(R.mipmap.ic_launcher);  // 配置error图标 如果不介意使用lib自带的图标，并不一定要调用这个API
        imageWatcher.setHintMode(ImageWatcher.TEXT);//设置指示器（默认小白点）
//        imageWatcher.setOnPictureLongPressListener(this); // 长按图片的回调，你可以显示一个框继续提供一些复制，发送等功能
        imageWatcher.setLoader(new ImageWatcher.Loader() {//调用show方法前，请先调用setLoader 给ImageWatcher提供加载图片的实现
            @Override
            public void load(Context context, String url, final ImageWatcher.LoadCallback lc) {
                Glide.with(context).asBitmap().load(url).into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                        lc.onResourceReady(resource);
                    }

                    @Override
                    public void onLoadStarted(Drawable placeholder) {
                        lc.onLoadStarted(placeholder);
                    }

                    @Override
                    public void onLoadFailed(Drawable errorDrawable) {
                        lc.onLoadFailed(errorDrawable);
                    }
                });
            }
        });
    }

    @Override
    public void onCompleted() {

    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public void onNext(FuliBean fuliBean) {
        adpter.setData(fuliBean);
//        Log.e(TAG, "onNext: " );
    }

    @Override
    public void onClickPic(int pos, FuliBean bean, ImageView imageView) {
        List<ImageView> ivlist = new ArrayList<>(bean.getResults().size());
        List<String> urlList = new ArrayList<>(bean.getResults().size());
        ImageView ivg;
        for (int i = 0; i < bean.getResults().size(); i++) {
            if (i==pos){
                ivg = imageView;
            }else {
                ivg = new ImageView(this);
            }
            ivlist.add(ivg);
            urlList.add(bean.getResults().get(i).getUrl());
            Log.e(TAG, "onClickPic: "+bean.getResults().get(i).getUrl());
        }
        imageWatcher.show(ivlist.get(pos), ivlist, urlList);
//        Toast.makeText(this,"imagewatcher",Toast.LENGTH_SHORT).show();
    }
}
